import apiSendMobileCode from '../../yali-sdk/business/live/api/apiSendMobileCode';
import apiGetWxOpenId from '../../yali-sdk/business/live/api/apiGetWxOpenId';
import apiBandMobileCode from '../../yali-sdk/business/live/api/apiBandMobileCode';
import { SUCCESS } from '../../yali-sdk/utils/success';
import { FAIL } from '../../yali-sdk/utils/fail';
import getIn from "../../yali-sdk/utils/getIn";
import {getExtConfig} from "../../utils/checkAuth";
let timer = '';
Page({
    data: {
        num: 0,
        is_send_code: false,
        is_auth: false,
        count_down: 0,
        submit: {},
        uid: '',
    },
    onLoad(options) {
        const self = this;
        const code = self.randomNumber();
        if (code && code !== undefined) {
            wx.setStorage({ key: 'code', data: code });
        }
        self.setData({ code, uid: options.uid ? options.uid : '' });
       // console.log(this,'this________________________');
    },
    onUnload() {
        const self = this;
        wx.setStorage({ key: 'code', data: '' });
    },
    changeCode() {
        const self = this;
        self.setData({ code: self.randomNumber() });
    },
    randomNumber() {
        const is_change_code = Math.random().toString(16).substr(2, 4);
        return is_change_code;
    },
    bandMobile(e) {
        const phone = e.detail.value;
        phone ? this.setData({ 'submit.phone': e.detail.value }) : this.setData({ 'submit.phone': '' });
       // console.log(this,'phone11');
    },
    bandCode(e) {
        const code = e.detail.value;
        const self = this;
        const phone = self.data.submit.phone;
     // console.log(self.data,'dsfsf');
        if (phone) {
            code && code.length == 4 ? self.setData({ 'submit.code': code, is_send_code: true }) : self.setData({ 'submit.code': '', is_send_code: false });
        }
    },
    bandMobileCode(e) {
        const self = this;
        self.setData({
            'submit.mobile_code': e.detail.value,
            is_auth: true,
        });
    },
    sendMobileCode(e) {
        try {
            const self = this;
            const code = wx.getStorageSync('code');
            if (code) {
                const input_code = self.data.submit.code;
                if (code !== input_code) { // 检测动态验证码是否正确
                    wx.showModal({
                        content: '动态验证码错误' });
                    return;
                }
                wx.showLoading();
                if (!(/^1[34578]\d{9}$/.test(self.data.submit.phone))) {
                    wx.hideLoading();
                    wx.showModal({
                        content: '手机号码格式错误' });
                    return;
                }
                global.actionDispatcher.execute(apiSendMobileCode, {
                    mobile: self.data.submit.phone,
                    uid: self.data.uid,
                }, (f, fs) => {
                    if (f === SUCCESS) {
                        const _this = this;
                        if (_this.data.count_down < 1) {
                            clearInterval(timer);
                            _this.data.count_down = 61;
                            timer = setInterval(() => {
                                const now_count = _this.data.count_down - 1;
                                if (now_count != -1) {
                                    _this.setData({ count_down: now_count });
                                } else {
                                    clearInterval(timer);
                                }
                            }, 1000);
                        }
                        wx.hideLoading();
                        wx.showToast({
                            title: '验证码发送成功',
                            icon: 'none',
                            duration: 1500,
                        });
                    } else if (f === FAIL) {
                        wx.hideLoading();
                        wx.showToast({
                            title: '验证码发送失败',
                            icon: 'none',
                            duration: 1500,
                        });
                    }
                });
            }
        } catch (e) {
            wx.showToast({ title: '验证CODE异常', icon: 'none', duration: 1500 });
        }
    },
    is_token(e) {
        wx.showLoading();
        const self = this;
        if (self.data.submit.code) {
            const param = {
                mobile: self.data.submit.phone,
                code: self.data.submit.mobile_code,
                uid: self.data.uid,
            };
            wx.login({
                success: (res) => {
                    console.log(res, 'dddddddddddddddddd');
                    if (res.errMsg == 'login:ok') {
                        const appid = getIn(getExtConfig(), ['extAppid']);
                        global.actionDispatcher.execute(apiGetWxOpenId, { code: res.code, appid }, (f, fs) => {
                            if (f === SUCCESS) {
                                param.openId = fs.data.openid;
                          //  param.expires_in=fs.data.expires_in;
                           // param.session_key=fs.data.session_key;
                                global.actionDispatcher.execute(apiBandMobileCode, param, (f, fs) => {
                                    if (f === SUCCESS) {
                                        wx.hideLoading();
                                        wx.showToast({
                                             title: '验证码发送成功',
                                             icon: 'none',
                                             duration: 1500,
                                         });
                                        const url = '/pages/anchor/anchor/anchor';
                                        wx.navigateTo({
                                             url,
                                         });
                                    } else if (f === FAIL) {
                                         wx.hideLoading();
                                         wx.showToast({ title: fs.msg, icon: 'none', duration: 1500 });
                                     }
                                });
                            } else if (f === FAIL) {
                                wx.hideLoading();
                                wx.showToast({ title: '获取openId失败', icon: 'none', duration: 1500 });
                            }
                        });
                    }
                },
                fail: (res) => {
                    wx.hideLoading();
                    wx.showToast({ title: '授权失败', icon: 'none', duration: 1500 });
                },
            });
        } else {
            wx.showToast({
                title: '请填写验证码',
                icon: 'none',
                duration: 1500,
            });
        }
    },
});
